//
//  JSMStrategyInfoViewController.m
//  TakeYouTourTogether
//
//  Created by king on 15/12/6.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMStrategyInfoViewController.h"
#import "JSMStrategyInfoTableViewCell.h"
#import "JSMStrategyContinentImageModel.h"
#import "JSMStrategyShowImageViewController.h"

@interface JSMStrategyInfoViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property(nonatomic, strong) BaseTableView *myTableView;
@property(nonatomic, strong) BaseImageView *myImageView;
@property(nonatomic, strong) UIVisualEffectView *effectView;
@property(nonatomic, strong) NSArray *dataImageArray;
@property(nonatomic, assign) CGFloat maxOffset;
@property(nonatomic, strong) UIButton *morePic;

@end

@implementation JSMStrategyInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self requestDataFromNetWorking];
}
#pragma mark - Method 创建更多图片按钮
- (void)createImagesArray {
    if (!_morePic) {
        self.morePic = [UIButton buttonWithType:UIButtonTypeCustom ];
        self.morePic.frame = CGRectMake(0, 0, 30, 30);
        [self.morePic addTarget:self action:@selector(moreAction) forControlEvents:UIControlEventTouchUpInside];
        [self.morePic setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:self.morePic];
        [self.morePic setBackgroundImage:[UIImage imageNamed:@"iconfont-images.png"] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = right;
    }
}
#pragma mark - Method tableView
- (BaseTableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        _myTableView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_myTableView];
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        _myTableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    }
    return _myTableView;
}
- (BaseImageView *)myImageView {
    if (!_myImageView) {
        _myImageView = [[BaseImageView alloc] init];
        _myImageView.clipsToBounds = YES;
        _myImageView.frame = CGRectMake(0, -200, SCREEN_WIDTH, 200);
        [self.myTableView addSubview:_myImageView];
        _myImageView.contentMode = UIViewContentModeScaleAspectFill;
        
        // 毛玻璃效果
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        self.effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
        self.effectView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);
        self.effectView.alpha = 0.6;
        // 添加子视图
        //    [self.view addSubview:effectView];
        [_myImageView addSubview:self.effectView];
    }
    return _myImageView;
}
#pragma mark - Method 设置拉伸效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat yOffset = scrollView.contentOffset.y;
    if (yOffset < 0) {
        self.myImageView.frame = CGRectMake(0, yOffset, SCREEN_WIDTH, -yOffset);
        self.effectView.alpha = 0.6+yOffset/1000*1.6;
    }
    if (yOffset < -300) {
        self.maxOffset = yOffset;
    }
    if (self.maxOffset < yOffset && yOffset < -200 ) {
        self.maxOffset = -200;
        [self moreAction];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *resue = @"strategyInfoTableViewResue";
    JSMStrategyInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];
    if (!cell) {
        cell = [[JSMStrategyInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resue];
    }
    if (indexPath.section == 0) {
        cell.titellabel.text = @"概况";
        cell.contentLabel.text = self.tempModel.des;
    }
    
    if (indexPath.section == 1) {
        cell.titellabel.text = @"地址";
        cell.contentLabel.text = self.tempModel.address;
    }
    
    if (indexPath.section == 2) {
        cell.titellabel.text = @"到达方式";
        cell.contentLabel.text = self.tempModel.arrival_type;
    }
    
    if (indexPath.section == 3) {
        cell.titellabel.text = @"开放时间";
        cell.contentLabel.text = self.tempModel.opening_time;
    }
    
    if (indexPath.section == 4) {
        cell.titellabel.text = @"门票价格";
        cell.contentLabel.text = self.tempModel.fee;
    }
    
    if (indexPath.section == 5) {
        cell.titellabel.text = @"联系方式";
        cell.contentLabel.text = self.tempModel.tel;
    }
    return cell;
}
#pragma mark - Method 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSString *str = @"asd";
    if (indexPath.section == 0) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
        CGRect rect = [self.tempModel.des boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height+100;
    } else if (indexPath.section == 1) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
        CGRect rect = [self.tempModel.address boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height+100;
    } else if (indexPath.section == 2) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
        CGRect rect = [self.tempModel.arrival_type boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height+100;
    } else if (indexPath.section == 3) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
        CGRect rect = [self.tempModel.opening_time boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height+100;
    } else if (indexPath.section == 4) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
        CGRect rect = [self.tempModel.fee boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height+100;
    } else {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
        CGRect rect = [self.tempModel.tel boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height+100;
    }
}
#pragma mark - Method requestDataFromNetWorking 网络请求数据
- (void)requestDataFromNetWorking {
    // url 没改
    NSString *strURL = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/5/%@/photos/?start=0&count=21&gallery_mode=true", self.tempModel.vid];
    //self.mtid
    //    NSLog(@"%@", strURL);
    [AFNetworkingTool getUrl:strURL hudSuperView:self.view result:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSLog(@"%@", dic);//elements data
        self.dataImageArray = [JSMStrategyContinentImageModel baseModelByArr:dic[@  "items"]];
        [self createImagesArray];
        [self.myImageView sd_setImageWithURL:[NSURL URLWithString:self.tempModel.cover] placeholderImage:[UIImage imageNamed:PLACE_HOLDER_IMAGE]];
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}
// 更多图片的按钮触发的方法
- (void)moreAction {
    
//    
    NSMutableArray *imgArray = [NSMutableArray arrayWithCapacity:self.dataImageArray.count];
//
//
    for (JSMStrategyContinentImageModel *more in self.dataImageArray) {
        NSURL *url = [NSURL URLWithString:more.photo_1600];
        [imgArray addObject:url];
    }
    
    JSMStrategyShowImageViewController *MoreVC = [[JSMStrategyShowImageViewController alloc] initWithSourceData:imgArray withIndex:0];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:MoreVC];
    
    [self presentViewController:naVC animated:YES completion:nil];
    
    
}
#pragma mark - Method didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
